//
//  WFSetViewController.m
//  谷居
//
//  Created by dllo on 16/6/30.
//  Copyright © 2016年 刘万方. All rights reserved.
//

#import "WFSetViewController.h"
#import "WFTableView.h"
#import "WFSetCell.h"
#import "WFSetCell_Third.h"
#import "WFSetCell_fourth.h"
#import "WFSetCell_second.h"
#import "StoreSandBox.h"
@interface WFSetViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UIButton *leftBarButton;
@property (nonatomic, retain) WFTableView *tableView;
@end

@implementation WFSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    // Do any additional setup after loading the view.
    self.navigationController.navigationItem.title = @"设置";
    
#pragma mark - 设置左侧按钮
    [self createLeftBarButtonItem];
    
#pragma mark - 穿件tableView
    [self createTableView];
}

/** leftBartbutton. */
- (void)createLeftBarButtonItem {
    _leftBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _leftBarButton.frame = CGRectMake(0, 0, 32, 32);
    [_leftBarButton setBackgroundImage:[UIImage imageNamed:@"返回2.png"] forState:UIControlStateNormal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftBarButton];
    [_leftBarButton release];
    [_leftBarButton addTarget:self action:@selector(pop:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

- (void)pop:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createTableView {
    _tableView = [[WFTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 48) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    [_tableView release];
    [_tableView registerClass:[WFSetCell class] forCellReuseIdentifier:@"first"];
    [_tableView registerClass:[WFSetCell_second class] forCellReuseIdentifier:@"second"];
    [_tableView registerClass:[WFSetCell_Third class] forCellReuseIdentifier:@"third"];
    [_tableView registerClass:[WFSetCell_fourth class] forCellReuseIdentifier:@"fourth"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    _tableView.rowHeight = 60;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        WFSetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"first" forIndexPath:indexPath];
        cell.leftImageView.image = [UIImage imageNamed:@"评价.png"];
        cell.rightLabel.text = @"给我评价";
        return cell;
        
    } else if (indexPath.row == 1) {
        WFSetCell_second *cell = [tableView dequeueReusableCellWithIdentifier:@"second" forIndexPath:indexPath];
        cell.leftImageView.image = [UIImage imageNamed:@"清理.png"];
        cell.rightLabel.text = @"清理缓存";
//        NSFileManager *manager = [NSFileManager defaultManager];
        NSString *cachesPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
        
        NSLog(@"%@", cachesPath);
        CGFloat size = [self folderSizeAtPath:cachesPath];
        cell.cachesLabel.text = [NSString stringWithFormat:@"%.1fM", size];
//        CGFloat size = [[StoreSandBox shareStoreSandBox] folderSizeAtPath:cachesPath];
//        cell.cachesLabel.text = [NSString stringWithFormat:@"%fM", size];
        return cell;
    
    } else if (indexPath.row == 2) {
        WFSetCell_Third *cell = [tableView dequeueReusableCellWithIdentifier:@"third" forIndexPath:indexPath];
        cell.leftImageView.image = [UIImage imageNamed:@"关于.png"];
        cell.rightLabel.text = @"关于谷居";
        cell.rightIamgeView.image = [UIImage imageNamed:@"右箭头.png"];
        return cell;
    
    } else {
        WFSetCell_fourth *cell = [tableView dequeueReusableCellWithIdentifier:@"fourth" forIndexPath:indexPath];
        if (indexPath.row == 3) {
            cell.midLabel.text = @"夜间模式";
        }else {
        cell.midLabel.text = @"退出登录";
        }
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 3) {
#pragma mark - 发送通知, 改为夜间模式
#pragma mark- NSUerDefaults
        /** 通过NSUserDefault 将全局变量存储到本地. */
        [[NSNotificationCenter defaultCenter] postNotificationName:@"夜间模式" object:nil];
        STATiC_IS_LONG = [[NSUserDefaults standardUserDefaults] integerForKey:@"夜间模式"];
        STATiC_IS_LONG = !STATiC_IS_LONG;
        [[NSUserDefaults standardUserDefaults] setInteger:STATiC_IS_LONG forKey:@"夜间模式"];

    }
    if (indexPath.row == 1) {
    NSString *cachesPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
        [self clearCache:cachesPath];
    }
}

- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}
- (float ) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}

-(void)clearCache:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isSave"];

    [_tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
